layui.use(['table', 'admin', 'ax', 'laydate', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem:'#startTime'
        ,value:new Date()
    });
    laydate.render({
        elem:'#endTime'
        ,value:new Date()
    });
    /**
     * 下架任务表管理
     */
    var HxStorageOffShelf = {
        tableId: "hxStorageOffShelfTable"
    };

    /**
     * 初始化表格的列
     */
    HxStorageOffShelf.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'status', sort: true, title: '状态', templet: '#statusTpl'},
            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'positionNumber', sort: true, title: '库位编号'},
            {field: 'positionType', sort: true, title: '库位类型'},
            {field: 'oldPositionNumber', sort: true, title: '原库位编号'},
            {field: 'oldPositionType', sort: true, title: '原库位类型'},
            {field: 'trayNumber', sort: true, title: '托盘编号'},
            {field: 'goodsName', sort: true, title: '商品名称'},
            {field: 'goodsNumber', sort: true, title: '商品编号'},
            {field: 'detailedDate', sort: true, title: '生产日期', totalRowText: '合计：'},
            {field: 'kcNum', sort: true, title: '库存数量', totalRowText: sl.kcNum},
            {field: 'receiveTime', sort: true, title: '领取时间'},
            {field: 'offUserName', sort: true, title: '下架人'},
            {field: 'offTime', sort: true, title: '下架时间'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'createUserName', sort: true, title: '创建人'},
            {field: 'returnStatus', sort: true, title: '回传状态'},
            {field: 'taskFlag', sort: true, title: '任务标记'},
            {field: 'tovoidTime', sort: true, title: '作废时间'},
            {field: 'tovoidUserName', sort: true, title: '作废人'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    HxStorageOffShelf.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['condition'] = $("#condition").val();
        queryData['returnStatus'] = $("#storageState").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['status'] = $("#status").val();
        queryData['taskFlag'] = $("#taskFlag").val();
	    $.ajax({
	        url:Feng.ctxPath + '/hxStorageOffShelf/count',
            data: queryData, // 将 queryData 作为请求体发送
	        dataType:"json",   //返回格式为json
	        async: false,
	        type:"post",   //请求方式
	        success:function(data){
	            sl = data.data
	        }
	    });
	
	    // 渲染表格
	    var tableResult = table.render({
	        elem: '#' + HxStorageOffShelf.tableId,
            where: queryData,
	        url: Feng.ctxPath + '/hxStorageOffShelf/list',
	        page: true,
	        limit: 20,
	        height: "full-60",
	        toolbar: '#toolbarfrom',
	        totalRow: true ,//开启合计行
	        cellMinWidth: 100,
	        cols: HxStorageOffShelf.initColumn()
	    });
    };

    /**
     * 弹出添加对话框
     */
    HxStorageOffShelf.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/hxStorageOffShelf/add';
    };

    HxStorageOffShelf.exportExcel = function () {
        var checkRows = table.checkStatus(HxStorageOffShelf.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
	        var queryData = {};
	        queryData['startTime'] = $("#startTime").val();
	        queryData['endTime'] = $("#endTime").val();
	        queryData['condition'] = $("#condition").val();
	        queryData['returnStatus'] = $("#storageState").val();
	        queryData['status'] = $("#status").val();
            queryData['taskFlag'] = $("#taskFlag").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/hxStorageOffShelf/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    HxStorageOffShelf.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/hxStorageOffShelf/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    HxStorageOffShelf.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hxStorageOffShelf/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(HxStorageOffShelf.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    var sl = {
        kcNum:""
    }

    $.ajax({
        url:Feng.ctxPath + '/hxStorageOffShelf/count',
        dataType:"json",   //返回格式为json
        async: false,
        type:"post",   //请求方式
        success:function(data){
            sl = data.data
        }
    });

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxStorageOffShelf.tableId,
        url: Feng.ctxPath + '/hxStorageOffShelf/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        totalRow: true ,//开启合计行
        cellMinWidth: 100,
        cols: HxStorageOffShelf.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxStorageOffShelf.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        HxStorageOffShelf.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        HxStorageOffShelf.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + HxStorageOffShelf.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            HxStorageOffShelf.openEditDlg(data);
        } else if (layEvent === 'delete') {
            HxStorageOffShelf.onDeleteItem(data);
        }
    });

     // 批量作废
     $('#btnTovoid').click(function () {
    	var checkStatus = table.checkStatus('hxStorageOffShelfTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
            	ids.push(data[i].id);
			}
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/hxStorageOffShelf/tovoid?ids="+ids, function (data) {
		        	if (data.success) {
	                    Feng.success("作废成功!");
	                	table.reload(HxStorageOffShelf.tableId);
					} else {
		                Feng.error("作废失败！"+data.message);
					}
                }, function (data) {
                    Feng.error("作废失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否作废选中订单?", operation);
		} else {
			layer.alert('请至少选中一条数据');
		}
     });
});
